package com.hwd.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwd.gmall.product.mapper.BaseCategory2Mapper;
import com.hwd.gmall.model.product.BaseCategory2;
import com.hwd.gmall.product.service.BaseCategory2Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 黄伟东/Victor
 * @date 2022/4/17 20:42
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseCategory2ServiceImpl implements BaseCategory2Service {

    @Resource
    private BaseCategory2Mapper baseCategory2Mapper;

    /**
     * 根据id查询二级分类
     *
     * @param id 二级分类id
     * @return com.hwd.gmall.model.product.BaseCategory2
     */
    @Override
    public BaseCategory2 getById(Long id) {
        return baseCategory2Mapper.selectById(id);
    }

    /**
     * 查询所有二级分类
     *
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory2>
     */
    @Override
    public List<BaseCategory2> listCategory2s() {
        return baseCategory2Mapper.selectList(null);
    }

    /**
     * 新增二级分类
     *
     * @param baseCategory2 二级分类
     */
    @Override
    public void saveCategory2(BaseCategory2 baseCategory2) {
        int insert = baseCategory2Mapper.insert(baseCategory2);
        if (insert <= 0) {
            throw new RuntimeException("新增二级分类失败");
        }
    }

    /**
     * 根据id删除二级分类
     *
     * @param id 二级分类
     */
    @Override
    public void deleteCategory2(Long id) {
        int delete = baseCategory2Mapper.deleteById(id);
        if (delete <= 0) {
            throw new RuntimeException("删除二级分类失败");
        }
    }

    /**
     * 根据id修改二级分类
     *
     * @param baseCategory2 二级分类
     */
    @Override
    public void updateCategory2(BaseCategory2 baseCategory2) {
        int update = baseCategory2Mapper.updateById(baseCategory2);
        if (update <= 0) {
            throw new RuntimeException("修改二级分类失败");
        }
    }

    /**
     * 分页查询
     *
     * @param page 当前页数
     * @param size 每页显示数量
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory2>
     */
    @Override
    public IPage<BaseCategory2> page(Integer page, Integer size) {
        return baseCategory2Mapper.selectPage(new Page<>(page, size), null);
    }

    /**
     * 条件查询
     *
     * @param baseCategory2 查询条件
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory2>
     */
    @Override
    public List<BaseCategory2> search(BaseCategory2 baseCategory2) {
        LambdaQueryWrapper<BaseCategory2> wrapper = buildQueryParam(baseCategory2);
        return baseCategory2Mapper.selectList(wrapper);
    }

    /**
     * 分页条件查询
     *
     * @param baseCategory2 查询条件
     * @param page          当前页数
     * @param size          每页显示数量
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory2>
     */
    @Override
    public IPage<BaseCategory2> search(BaseCategory2 baseCategory2, Integer page, Integer size) {
        LambdaQueryWrapper<BaseCategory2> wrapper = buildQueryParam(baseCategory2);
        return baseCategory2Mapper.selectPage(new Page<>(page, size), wrapper);
    }

    /**
     * 条件构造
     *
     * @param baseCategory2 查询条件
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.hwd.gmall.model.product.BaseCategory2>
     */
    private LambdaQueryWrapper<BaseCategory2> buildQueryParam(BaseCategory2 baseCategory2) {
        return new LambdaQueryWrapper<BaseCategory2>()
                .eq(Objects.nonNull(baseCategory2.getId()), BaseCategory2::getId, baseCategory2.getId())
                .like(StringUtils.isNotEmpty(baseCategory2.getName()), BaseCategory2::getName, baseCategory2.getName());
    }
}
